package com.huiquan.inspection.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.huiquan.framework.base.BaseController;
import com.huiquan.util.StringUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.huiquan.analysis.domain.User;
import com.huiquan.framework.base.ReturnData;
import com.huiquan.framework.utils.RequestParamUtil;
import com.huiquan.inspection.service.IndexService;
import com.huiquan.inspection.service.ReportService;
import com.huiquan.inspection.service.TestingClassService;

@Controller
public class InspectionController extends BaseController {

	@Autowired
	private IndexService indexService;

	@Autowired
	private ReportService reportService;

	@Autowired
	private TestingClassService testingClassService;


	/**
	 * 指标模块的指标界面
	 * 
	 * @param tabid
	 * @return
	 */
	@RequestMapping(value = "/inspection/indexes_list.htm")
	public ModelAndView indexsList(String tabid){
		// 维护页面参数到session
		Map<String, Object> pageParam;
		if (StringUtil.isNotEmpty(tabid)) {
			pageParam = new HashMap<>();
			pageParam.put("tabid", tabid);
			pageParam.put("exactSearchFlag", "0");
			setSessionValue("pageParam", pageParam);
		} else {
			pageParam = (Map<String, Object>) getSessionValue("pageParam");
		}

		String start = RequestParamUtil.getParamByRequestOrSession(getRequest(), pageParam, "start");
		String exactSearchFlag = RequestParamUtil.getParamByRequestOrSession(getRequest(), pageParam, "exactSearchFlag");
		String countPerPageStr = RequestParamUtil.getParamByRequestOrSession(getRequest(), pageParam, "countPerPage");
		String name = RequestParamUtil.getParamByRequestOrSession(getRequest(), pageParam, "name");
		String originalName = RequestParamUtil.getParamByRequestOrSession(getRequest(), pageParam, "originalName");
		String blankFlag = RequestParamUtil.getParamByRequestOrSession(getRequest(), pageParam, "blankFlag");

		Map<String, Object> map = indexService.getList(start, name, originalName, exactSearchFlag, blankFlag,
				countPerPageStr);
		return new ModelAndView("inspection/indexes_list", map);
	}

	/**
	 * 指标模块的报告界面
	 * 
	 * @param tabid
	 * @return
	 */
	@RequestMapping(value = "/inspection/report_list.htm")
	public ModelAndView reportList(String tabid){
		// 维护页面参数到session
		Map<String, Object> pageParam;
		if (StringUtil.isNotEmpty(tabid)) {
			pageParam = new HashMap<>();
			pageParam.put("tabid", tabid);
			pageParam.put("exactSearchFlag", "0");
			setSessionValue("pageParam", pageParam);
		} else {
			pageParam = (Map<String, Object>) getSessionValue("pageParam");
		}

		String start = RequestParamUtil.getParamByRequestOrSession(getRequest(), pageParam, "start");
		String exactSearchFlag = RequestParamUtil.getParamByRequestOrSession(getRequest(), pageParam, "exactSearchFlag");
		String keyword = RequestParamUtil.getParamByRequestOrSession(getRequest(), pageParam, "keyword");
		String countPerPageStr = RequestParamUtil.getParamByRequestOrSession(getRequest(), pageParam, "countPerPage");

		Map<String, Object> map = reportService.getList(start, keyword, countPerPageStr, exactSearchFlag);
		return new ModelAndView("inspection/report_list", map);
	}

	/**
	 * 指标模块的检查大类界面
	 *
	 * @param tabid
	 * @return
	 */
	@RequestMapping(value = "/inspection/testingClass_list.htm")
	public ModelAndView testingClassList(String tabid){
		// 维护页面参数到session
		Map<String, Object> pageParam;
		if (StringUtil.isNotEmpty(tabid)) {
			pageParam = new HashMap<>();
			pageParam.put("tabid", tabid);
			pageParam.put("exactSearchFlag", "0");
			setSessionValue("pageParam", pageParam);
		} else {
			pageParam = (Map<String, Object>) getSessionValue("pageParam");
		}
		String start = RequestParamUtil.getParamByRequestOrSession(getRequest(), pageParam, "start");
		String exactSearchFlag = RequestParamUtil.getParamByRequestOrSession(getRequest(), pageParam, "exactSearchFlag");
		String keyword = RequestParamUtil.getParamByRequestOrSession(getRequest(), pageParam, "keyword");
		String countPerPageStr = RequestParamUtil.getParamByRequestOrSession(getRequest(), pageParam, "countPerPage");

		Map<String, Object> map = testingClassService.getList(start, keyword, countPerPageStr, exactSearchFlag);
		return new ModelAndView("inspection/testingClass_list", map);
	}

	/**
	 * 新增或修改指标
	 *
	 * @return
	 * @throws IOException
	 */
	@ResponseBody
	@RequestMapping(value = "/inspection/insert_or_update.do")
	public ReturnData insertOrUpdateIndex(){
		String originalId = getRequestValue("originalId");
		String classId = getRequestValue("classId");
		String normalName = getRequestValue("normalName").trim();
		String mutiBox = getRequestValue("mutiBox");
		String implication = getRequestValue("implication");
		String normalValue = getRequestValue("normalValue");
		String clinicalSign = getRequestValue("clinicalSign");
		String attention = getRequestValue("attention");
		User user = (User) getSessionValue("user");

		// 暂时没有新增
		return indexService.edit(originalId, classId, normalName, mutiBox, implication, normalValue, clinicalSign,
				attention, user);
	}

	/**
	 * 新增检验大类
	 *
	 * @param image
	 * @return
	 * @throws IOException
	 */
	@ResponseBody
	@RequestMapping(value = "/inspection/insert_testing_class.do")
	public ReturnData insertTestingClass(@RequestParam(required = false) MultipartFile image) throws IOException {
		User user = (User) getSessionValue("user");
		Map<String, String> params = new HashMap<>();
		params.put("id", getRequestValue("id"));
		params.put("rank", getRequestValue("rank"));
		params.put("name", getRequestValue("name"));
		params.put("disable", getRequestValue("disable"));
		return testingClassService.insert(params, user, image);
	}

	/**
	 * 修改检验大类
	 *
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/inspection/update_testing_class.do")
	public ReturnData updateTestingClass() {
		String id = getRequestValue("id");
		String name = getRequestValue("name");
		String imageName = getRequestValue("imageName");
		User user = (User) getSessionValue("user");

		return testingClassService.update(id, name, imageName, user);
	}

	/**
	 * 修改检验大类对应的图片
	 * 
	 * @param id
	 * @param file
	 * @return
	 * @throws IOException
	 * @throws IllegalStateException
	 */
	@ResponseBody
	@RequestMapping("/inspection/update_testing_class_image.do")
	public ReturnData updateTestingClassImage(String id,
			@RequestParam(required = false) MultipartFile file) throws IllegalStateException, IOException {
		User user = (User) getSessionValue("user");
		return testingClassService.updateImage(id, user, file);
	}

	/**
	 * 修改检验大类对应的disable
	 *
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/inspection/update_testing_class_disabe.do")
	public ReturnData updateTestingClassDisable() {
		User user = (User) getSessionValue("user");
		String id = getRequestValue("id");
		String disable = getRequestValue("disable");
		return testingClassService.updateDisable(id, user, disable);
	}

	/**
	 * 移动检验大类的位置，调整其排序
	 *
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/inspection/move_testing_class.do")
	public ReturnData moveTestingClass() {
		User user = (User) getSessionValue("user");
		String id = getRequestValue("id");
		String flag = getRequestValue("flag");
		return testingClassService.move(user, id, Integer.valueOf(flag));
	}

	/**
	 * 批量插入检验大类
	 *
	 * @param batchFile
	 * @return
	 * @throws IOException
	 * @throws IllegalStateException
	 */
	@ResponseBody
	@RequestMapping("/inspection/batch_add_testing_class.do")
	public ReturnData batchAddTestingClass(@RequestParam(required = false) MultipartFile batchFile) throws IllegalStateException, IOException {
		User user = (User) getSessionValue("user");
		return testingClassService.batchAddTestingClass(user, batchFile);
	}

	/**
	 * 删除一条Testing
	 *
	 * @param resp
	 * @return
	 * @throws IOException
	 */
	@RequestMapping("/inspection/delete_testing_class.do")
	public void deleteTestingClass(String id, HttpServletResponse resp) throws IOException {
		testingClassService.deleteClass(id);
		resp.sendRedirect("/inspection/testingClass_list.htm?tabid=26_23");
	}

	/**
	 * 修改指标预处理
	 *
	 * @return
	 * @throws IOException
	 */
	@RequestMapping(value = "/inspection/indexes_pre_edit.htm")
	public ModelAndView preEdit(){
		User user = (User) getSessionValue("user");

		String id = getRequestValue("id");
		String name = getRequestValue("name");
		String indexId = getRequestValue("indexId");
		String classId = getRequestValue("classId");
		String originalName = getRequestValue("originalName");
		String encyclopediaId = getRequestValue("encyclopediaId");
        Map<String, Object> map = indexService.preEdit(id, user, name, originalName, encyclopediaId, indexId, classId);
        return new ModelAndView("inspection/indexes_edit", map);
	}

	/**
	 * 根据指标信息获取指标百科
	 * 
	 * @param req
	 * @param resp
	 * @return
	 * @throws IOException
	 */
	@ResponseBody
	@RequestMapping(value = "/inspection/get_encyclopedia_by_index.do")
	public ReturnData getEncyclopediaByIndex(HttpServletRequest req, HttpServletResponse resp) throws IOException {

		String classId = getRequestValue("classId");
		String normalName = getRequestValue("normalName");
		return indexService.getEncyclopediaByIndex(classId, normalName);
	}

	/**
	 * 将指标提交到服务器
	 *
	 * @return
	 * @throws IOException
	 */
	@ResponseBody
	@RequestMapping(value = "/inspection/submit_server.do")
	public ReturnData submitServer() throws Exception {
		return indexService.submitServer();
	}

	/**
	 * 导入百科
	 *
	 * @return
	 * @throws IOException
	 */
	@ResponseBody
	@RequestMapping(value = "/inspection/encyclopedia_import.do")
	public ReturnData encyclopediaImport(@RequestParam(required = false) MultipartFile file)
			throws Exception {
		User user = (User) getSessionValue("user");
		return indexService.encyclopediaImport(file, user);
	}

	/**
	 * 导出百科
	 */
	@ResponseBody
	@RequestMapping(value = "/inspection/encyclopedia_export.htm")
	public void encyclopediaExport(HttpServletResponse resp) throws Exception {
		indexService.encyclopediaExport(resp);
	}

	/**
	 * 同步同义词
	 */
	@ResponseBody
	@RequestMapping(value = "/inspection/init_synonymy.do")
	public ReturnData initSynonymy() throws Exception {
		return indexService.initSynonymy();
	}
}
